package com.example.springbootjwtexample.koal;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {

    public static byte[] SHA256HMAC(byte[] key, byte[] content){
        String algorithm = "HmacSHA256";
        byte[] result = null;
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key,algorithm);
            Mac mac = Mac.getInstance(algorithm);
            mac.init(keySpec);
            result = mac.doFinal(content);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static byte[] SHA256HMACforString(String key, String content){
        return SHA256HMAC(ByteUtil.hexStringToBytes(key), content.getBytes());
    }

    public static byte[] SHA256HMACforHexString(String key, String content){
        return SHA256HMAC(ByteUtil.hexStringToBytes(key),ByteUtil.hexStringToBytes(content));
    }

}
